package com.example.handheld4_gsignal_app.ui.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.handheld4_gsignal_app.R;
import com.example.handheld4_gsignal_app.data.Task;
import com.example.handheld4_gsignal_app.services.TaskService;
import com.example.handheld4_gsignal_app.ui.adapters.TaskListAdapter;
import com.example.handheld4_gsignal_app.ui.adapters.TaskKanbanAdapter;
import java.util.ArrayList;
import java.util.List;

/**
 * 任务看板Fragment - 任务管理和看板视图
 */
public class TaskBoardFragment extends Fragment {

    private ListView taskListView;
    private Spinner viewModeSpinner;
    private TaskListAdapter taskListAdapter;
    private TaskKanbanAdapter taskKanbanAdapter;
    private TaskService taskService;
    private List<Task> tasks;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_task_board, container, false);
        
        initViews(view);
        setupViewModeSpinner();
        initTaskService();
        loadTaskData();
        
        return view;
    }

    /**
     * 初始化视图组件
     */
    private void initViews(View view) {
        taskListView = view.findViewById(R.id.task_list_view);
        viewModeSpinner = view.findViewById(R.id.view_mode_spinner);
    }

    /**
     * 设置视图模式下拉框
     */
    private void setupViewModeSpinner() {
        String[] viewModes = {"看板视图", "列表视图"};
        String[] viewIcons = {"📊", "📋"};
        
        // 创建自定义适配器
        ArrayAdapter<String> viewModeAdapter = new ArrayAdapter<String>(getContext(), 
            R.layout.custom_spinner_item, R.id.spinner_text, viewModes) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                TextView iconText = view.findViewById(R.id.spinner_icon);
                TextView textView = view.findViewById(R.id.spinner_text);
                
                if (iconText != null) {
                    iconText.setText(viewIcons[position]);
                }
                if (textView != null) {
                    textView.setText(viewModes[position]);
                }
                return view;
            }
            
            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = LayoutInflater.from(getContext()).inflate(
                        R.layout.custom_spinner_dropdown_item, parent, false);
                }
                
                TextView iconText = convertView.findViewById(R.id.dropdown_icon);
                TextView textView = convertView.findViewById(R.id.dropdown_text);
                
                if (iconText != null) {
                    iconText.setText(viewIcons[position]);
                }
                if (textView != null) {
                    textView.setText(viewModes[position]);
                }
                return convertView;
            }
        };
        
        viewModeAdapter.setDropDownViewResource(R.layout.custom_spinner_dropdown_item);
        viewModeSpinner.setAdapter(viewModeAdapter);
        
        // 设置视图模式切换监听器
        viewModeSpinner.setOnItemSelectedListener(new android.widget.AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(android.widget.AdapterView<?> parent, View view, int position, long id) {
                refreshData();
            }

            @Override
            public void onNothingSelected(android.widget.AdapterView<?> parent) {
                // 不做处理
            }
        });
    }

    /**
     * 初始化任务服务
     */
    private void initTaskService() {
        taskService = new TaskService(getContext());
        tasks = new ArrayList<>();
    }

    /**
     * 加载任务数据
     */
    private void loadTaskData() {
        if (taskService != null) {
            tasks = taskService.getAllTasks();
            updateTaskList();
        }
    }

    /**
     * 更新任务列表
     */
    private void updateTaskList() {
        // 根据当前选择的视图模式来显示
        if (viewModeSpinner != null) {
            String viewMode = viewModeSpinner.getSelectedItem().toString();
            if ("看板视图".equals(viewMode)) {
                updateTaskListForKanban();
            } else {
                updateTaskListForList();
            }
        } else {
            // 默认使用列表视图
            updateTaskListForList();
        }
    }

    /**
     * 根据视图模式刷新数据
     */
    private void refreshData() {
        String viewMode = viewModeSpinner.getSelectedItem().toString();
        
        if (taskService != null) {
            if ("看板视图".equals(viewMode)) {
                // 按状态分组显示任务
                tasks = taskService.getTasksByStatus();
                updateTaskListForKanban();
            } else {
                // 列表视图显示所有任务
                tasks = taskService.getAllTasks();
                updateTaskListForList();
            }
        }
    }

    /**
     * 更新看板视图的任务列表
     */
    private void updateTaskListForKanban() {
        if (taskKanbanAdapter == null) {
            taskKanbanAdapter = new TaskKanbanAdapter(getContext(), tasks);
        } else {
            taskKanbanAdapter.updateTasks(tasks);
        }
        taskListView.setAdapter(taskKanbanAdapter);
    }

    /**
     * 更新列表视图的任务列表
     */
    private void updateTaskListForList() {
        if (taskListAdapter == null) {
            taskListAdapter = new TaskListAdapter(getContext(), tasks);
        } else {
            taskListAdapter.updateTasks(tasks);
        }
        taskListView.setAdapter(taskListAdapter);
    }
}